package com.example.mvpdemo.demo;

import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.TextView;

import com.example.mvpdemo.R;
import com.example.mvpdemo.base.BaseMvpActivity;
import com.example.mvpdemo.dao.manager.DBManager;
import com.example.mvpdemo.dao.model.User;

public class MainActivity extends BaseMvpActivity<MainPresenter> implements MainContract.MainView {

    private static final String TAG = "MainActivity";
    private TextView textClick;

    @Override
    protected void initPresenter() {
        mPresenter = new MainPresenter();
        mPresenter.addContext(this);
        mPresenter.attachView(this);
    }

    @Override
    protected void fillLayout() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void initView() {
        textClick = findViewById(R.id.click);
        textClick.setOnClickListener(v -> {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.fl_content, new HomeFragment()).addToBackStack(null).commit();
        });

        findViewById(R.id.btn_click_one).setOnClickListener(v -> {
            Log.e(TAG, "click button one");
            for (int i = 0; i < 100; i++) {
                User user = new User("张无忌" + i, i + "");
                long count = DBManager.getInstance().getUserDao().insertOrReplace(user);
                Log.e(TAG, count + "");
            }
        });
        findViewById(R.id.btn_click_two).setOnClickListener(v -> {
            Log.e(TAG, "click button two");
        });

    }

    @Override
    public void loadUserSucc(User user) {
        Log.e(TAG, user.toString());
        dismissDialog();
    }

    @Override
    public void loadUserError() {

    }

}
